/////////////////////////////////////////////////////////////////////////////
// Name:        SourceCatListBox.cpp
// Purpose:     
// Author:      yospaly
// Modified by: 
// Created:     26/12/2008 14:23:16
// RCS-ID:      
// Copyright:   Copyright (C) 2008, yospaly
// Licence:     
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
////@end includes

#include "SourceCatListBox.h"
#include "inibuilder.h"
#include "events.h"

////@begin XPM images
////@end XPM images


/*!
 * SourceCatListBox type definition
 */

IMPLEMENT_DYNAMIC_CLASS( SourceCatListBox, CategoryListBox )


/*!
 * SourceCatListBox event table definition
 */

BEGIN_EVENT_TABLE( SourceCatListBox, CategoryListBox )

////@begin SourceCatListBox event table entries
    EVT_LISTBOX( ID_SOURCECAT_LISTBOX, SourceCatListBox::OnSelected )

////@end SourceCatListBox event table entries

END_EVENT_TABLE()


/*!
 * SourceCatListBox constructors
 */

SourceCatListBox::SourceCatListBox()
{
    Init();
}

SourceCatListBox::SourceCatListBox(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, const wxArrayString& choices, long style, const wxValidator& validator)
{
    Init();
    Create(parent, id, pos, size, choices, style, validator);
}


/*!
 * SourceCatListBox creator
 */

bool SourceCatListBox::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, const wxArrayString& choices, long style, const wxValidator& validator)
{
////@begin SourceCatListBox creation
    CategoryListBox::Create(parent, id, pos, size, choices, style, validator);
    CreateControls();
////@end SourceCatListBox creation
    return true;
}


/*!
 * SourceCatListBox destructor
 */

SourceCatListBox::~SourceCatListBox()
{
////@begin SourceCatListBox destruction
////@end SourceCatListBox destruction
}


/*!
 * Member initialisation
 */

void SourceCatListBox::Init()
{
////@begin SourceCatListBox member initialisation
////@end SourceCatListBox member initialisation
}


/*!
 * Control creation for SourceCatListBox
 */

void SourceCatListBox::CreateControls()
{    
////@begin SourceCatListBox content construction
    this->SetFont(wxFont(10, wxSWISS, wxNORMAL, wxNORMAL, false, wxT("Verdana")));
////@end SourceCatListBox content construction
}

void SourceCatListBox::ShowCategories() {
    IniBuilder& inibuilder = IniBuilder::get();
    
    this->Clear();
    this->Append(this->default_items);
    
    for( int i=0; i<inibuilder.cat_names.GetCount(); i++ ) {
    	wxString cat = inibuilder.cat_names[i];
    	CatInfo& cat_info = inibuilder.categories[cat];
    	
    	int count = cat_info.pkg_names.GetCount();
    	
    	for (int j=0; j<cat_info.pkg_names.GetCount(); j++) {
        	PkgInfo& pkg_info = inibuilder.packages[cat_info.pkg_names[j]];
    		if (pkg_info.source_path.IsEmpty()) {
    			count--;
    		}
    	}
    	
    	this->Append(wxString::Format(_T("[Source] %s (%d)"), cat.c_str(), count));
    }
    
	this->SetSelection(0);
}

/*!
 * wxEVT_COMMAND_LISTBOX_SELECTED event handler for ID_SOURCECAT_LISTBOX
 */

void SourceCatListBox::OnSelected( wxCommandEvent& event )
{
    wxString cat = this->GetStringSelection();
    
    cat.Replace(_T("[Source] "), _T(""), false);
    
    if (0==cat.CmpNoCase(_T("All")) || 0==cat.CmpNoCase(_T("Installed"))
    	|| 0==cat.CmpNoCase(_T("Upgradable")) || 0==cat.CmpNoCase(_T("Not Installed")) ) {
    	EventCenter::post(wxEVT_PKGSRC_FILTER_EVENT, cat);
    } else {
		int i = cat.Find(_T(' '));
		if (i!=wxNOT_FOUND) {
			EventCenter::post(wxEVT_PKGSRC_FILTER_EVENT, cat.SubString(0,i-1));
			// EventCenter::post(wxEVT_PKG_FILTER_EVENT, _T("All"));
		}
    }
}

